<template>
  <div>
    <el-dialog
      title="新增字段"
      :visible.sync="show"
      width="30%"
      append-to-body
      @close="handleClickDeal('cancel')"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="formRules">
        <el-form-item label="字段描述" prop="name">
          <el-input v-model="form.name" :disabled="type=='check'"/>
        </el-form-item>
        <el-form-item label="字段编码" prop="value">
          <el-input v-model="form.value" :disabled="type=='check'"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="isLoading" plain @click="handleClickDeal('cancel')">取 消</el-button>
        <el-button type="primary" :loading="isLoading" @click="handleClickDeal('submit')"
                   v-if="type !='check'">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import field from '@/api/wf/field'
  import msgUtil from '@/utils/msgUtil'

  export default {
    name: "field_detail",
    props: {
      postDetail: {
        show: false,
        type: '',
        params: {}
      }
    },
    watch: {
      postDetail(value) {
        this.show = value.show
        this.procDefKey = value.params.procDefKey ? value.params.procDefKey : ''
        this.detailId = value.params.detailId ? value.params.detailId : ''
        this.type = value.type
        this.init()
      }
    },
    data() {
      return {
        show: false,
        procDefKey: '',
        type: 'add',
        isLoading: false,
        detailId: '',
        form: {
          name: '',
          value: '',
          procDefKey: '',
        },
        formRules: {
          name: [
            {required: true, message: '请输入' + '字段描述', trigger: 'change'},
          ],
          value: [
            {required: true, message: '请输入' + '字段编码', trigger: 'change'},
          ],
        }
      }
    },
    methods: {
      init() {
        if (this.type != 'add') {
          this.getDetail()
        }
      },
      getDetail() {
        field.findOne(this.detailId).then(response => {
          this.form = response.data
        })
      },
      clearnForm() {
        this.form = {
          name: '',
          value: '',
          procDefKey: '',
        }
      },
      submitDetail() {
        if (this.type === 'add') {
          field.add(this.form).then(response => {
            msgUtil.success2('保存成功!')
          }).finally(() => {
            this.isLoading = false
            this.clearnForm()
            this.$emit("reloadList")
          })
        } else if (this.type === 'edit') {
          field.edit(this.form).then(res => {
            msgUtil.success2('修改成功')
          }).finally(() => {
            this.isLoading = false
            this.clearnForm()
            this.$emit("reloadList")
          })
        }
      },
      handleClickDeal(type) {
        switch (type) {
          case 'submit':
            this.$refs['form'].validate((valid) => {
              if (valid) {
                this.show = false
                this.isLoading = true
                this.form.procDefKey = this.procDefKey
                this.form.moduleId = '4028c9817810a020017810a138480000'
                this.submitDetail()
              }
            })
            break
          case 'cancel':
            this.show = false
            this.clearnForm()
            break
        }
      },
    }
  }
</script>

<style scoped>

</style>
